package com.example.factory;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.classreading.MetadataReader;

import java.io.IOException;
import java.util.Set;

/**
 * @ClassName MybatisSacnner
 * @Description
 * @Author Yin
 * @Date 2021/4/8 16:40
 * @Version 1.0
 */
public class MybatisScanner extends ClassPathBeanDefinitionScanner {
    public MybatisScanner(BeanDefinitionRegistry registry) {
        super(registry);
    } //扫描接口的@Componment

    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        return beanDefinition.getMetadata().isInterface(); //把是接口的传回来
    }

    @Override
    protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
        Set<BeanDefinitionHolder> beanDefinitionHolders = super.doScan(basePackages);
        for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
            BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
            beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(beanDefinition.getBeanClassName()); //动态地为构造方法传入参数值
            beanDefinition.setBeanClassName(MybatisFactoryConfig.class.getName());
        }
        return beanDefinitionHolders;
    }
}
